package com.LCQ.crm.workbench.service.impl;

import com.LCQ.crm.utils.SqlSessionUtil;
import com.LCQ.crm.vo.PaginationVo;
import com.LCQ.crm.workbench.dao.ActivityDao;
import com.LCQ.crm.workbench.dao.ActivityRemarkDao;
import com.LCQ.crm.workbench.domain.Activity;
import com.LCQ.crm.workbench.domain.ActivityRemark;
import com.LCQ.crm.workbench.service.ActivityService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActivityServiceImpl implements ActivityService {
    private ActivityDao activityDao = SqlSessionUtil.getSqlSession().getMapper(ActivityDao.class);
    private ActivityRemarkDao activityRemarkDao = SqlSessionUtil.getSqlSession().getMapper(ActivityRemarkDao.class);
    @Override
    //添加成功后会返回查询列表，添加失败返回null
    public int add(Activity activity) {
        return activityDao.insert(activity);
    }
    public List<Map> getPage(int pageNum,int pageSize){
        Map map = new HashMap();
        int index = pageNum*pageSize;
        map.put("index",index);
        map.put("pageSize",pageSize);
        return activityDao.selectPage(map);
    }

    @Override
    public Map getGlobal() {
        return activityDao.getGlobal();
    }

    @Override
    public PaginationVo<Activity> getPageList(Map paraMap) {
        PaginationVo<Activity> pageList = new PaginationVo<>();
        pageList.setPageList(activityDao.getPageListByCondition(paraMap));
        pageList.setTotal(activityDao.getPageListCountByCondition(paraMap));
        return pageList;
    }

    @Override
    public boolean delete(String[] ids) {
        //不仅删除活动表还会删除相关联的备注表
        int rCount = activityRemarkDao.getCountAIds(ids);
        int drCount = activityRemarkDao.deleteByAIds(ids);
        int aCount = activityDao.getCountByIds(ids);
        int adCount = 0;
        if(rCount==drCount){
            adCount = activityDao.delete(ids);
        }else{
            return false;
        }
        return adCount==aCount;
    }

    @Override
    public Activity getActivityById(String id) {
        return activityDao.getById(id);
    }

    @Override
    public int update(Activity activity) {
        return activityDao.update(activity);
    }

    @Override
    public Activity getDetail(String id) {
        return activityDao.getDetail(id);
    }

    @Override
    public List<ActivityRemark> getRemarkByAId(String id) {
        return activityRemarkDao.getById(id);
    }

    @Override
    public boolean deleteRemark(String id) {
        int result = activityRemarkDao.delete(id);
        return result==1;
    }

    @Override
    public boolean updateRemark(ActivityRemark activityRemark) {
        int result = activityRemarkDao.update(activityRemark);
        return result==1;
    }

    @Override
    public boolean addRemark(ActivityRemark activityRemark) {
        int result = activityRemarkDao.insert(activityRemark);
        return result==1;
    }
}
